<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <!--增加一个名字为title的插槽 -->
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>

    <!-- 频道列表 -->
    <!-- active 控制被激活的标签的索引（下标） -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 频道的文章列表 -->
        <article-list ref="article-list" :channel="channel" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn">
        <i @click="isChennelEditShow = true" class="iconfont icongengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑 -->
    <van-popup
      v-model="isChennelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <channel-edit
        @update-active="onUpdateActive"
        :active="active"
        :my-channels="channels"
      />
    </van-popup>
    <!-- /频道编辑 -->
  </div>
</template>

<script>
import { getUserChannels } from "@/api/user";
import ArticleList from "./components/article-list";
import ChannelEdit from "./components/channel-edit";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
export default {
  name: "HomePage",
  components: {
    // 文章列表
    ArticleList,
    // 弹出层组件
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      // 被控制的tabs索引
      active: 0,
      // 频道列表数据
      channels: [],
      // 汉堡按钮弹出层
      isChennelEditShow: false,
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    this.getHomeList();
  },
  mounted() {},
  methods: {
    async getHomeList() {
      try {
        let channles = [];
        const localChannels = getItem("TOUTIAO_CHANNELS");
        if (this.user || !localChannels) {
          // 登录 或者 本地没有存储 获取后端数据
          const { data } = await getUserChannels();
          this.channels = data.data.channels;
          return false;
        } else {
          // 未登录并且本地有数据
          channles = localChannels;
        }
        this.channels = channles;
      } catch (err) {
        this.$toast("获取频道列表数据失败");
      }
    },

    onUpdateActive(index, isChennelEditShow = true) {
      // 跟新激活的频道索引
      this.active = index;
      // 关闭汉堡弹出层
      this.isChennelEditShow = isChennelEditShow;
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  // 因为设计稿是375，效果是750，所以需要乘以2，这是搜索和tbs的高度乘以2
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  // 深度影响后代元素
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.iconfont {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
